import { _decorator, AnimationClip, Component, Label, Animation, Vec3, Sprite, BoxCollider2D, ERigidBody2DType, RigidBody2D, Size } from 'cc';
import { createPlaceholder, createUILabel, createUINode, destroyNode, getFrameAllAsset } from '../utils';
import DataManager from '../Runtime/DataManager';
const { ccclass} = _decorator;

@ccclass('ExplosionTimingManager')
export class ExplosionTimingManager extends Component {
    remainingTime: number = 10
    callback
    isBlast: Boolean = false //检测是否正在爆炸
    blast_clip: AnimationClip
    init(position: Vec3 = new Vec3(0, 30)) {
        const labelNode = createUINode("bombLabel")
        labelNode.setParent(this.node)
        labelNode.setPosition(position)
        createUILabel({ parentNode: labelNode, text: ' ', fontSize: 30, color: "#fbfbfd" })
    }
    onDestroy(): void {
        if (this.callback) {
            clearInterval(this.callback)
        }
    }
    playBombAnimation(isRole: Boolean = false,remainingTime:number=-1) {
        if (this.callback) return  //防止多次执行
        if(remainingTime!=-1){
            this.remainingTime =remainingTime
        }else{
            this.remainingTime =DataManager.Instance.explosionTime==0?DataManager.Instance.explosionTime:10
        }
        
        this.countDown(isRole)
        this.callback = setInterval(() => {
            this.countDown(isRole)
        }, 700)
    }
    countDown(isRole) {
        const label = this.node.getChildByName("bombLabel")?.getComponent(Label)
        if (label) {
            label.string = this.remainingTime + ''
            if (this.remainingTime >= 1) {
                this.remainingTime = this.remainingTime - 1
                if (isRole) {
                    DataManager.Instance.explosionTime = this.remainingTime
                }
            } else {
                this.blast()
                clearInterval(this.callback)
            }
        } else {
            clearInterval(this.callback)
        }
    }

    blast() {
        if (this.isBlast) return
        this.isBlast = true
        if (this.node.isValid && this.node) {
            const blastNode = createUINode("blastNode")
            blastNode.setParent(this.node)
            blastNode.setScale(2.4, 2.4)
            const animation = blastNode.addComponent(Animation)
            const sprite = blastNode.addComponent(Sprite)
            sprite.sizeMode = Sprite.SizeMode.TRIMMED
            animation.defaultClip = DataManager.Instance.blast_clip
            animation.play()
            DataManager.Instance.audioMgr.playOneShot(DataManager.Instance.audio.find(item => item.name == '1237'))
            animation.on(
                Animation.EventType.FINISHED,
                this.onBlastFinished,
                this
            );
            setTimeout(() => {
                if (this.node && this.node.isValid) {
                    const startNode = createUINode("blastStart")
                    startNode.setParent(this.node)
                    const rigidBody2D = startNode.addComponent(RigidBody2D)
                    rigidBody2D.type = ERigidBody2DType.Dynamic
                    rigidBody2D.fixedRotation = true
                    rigidBody2D.gravityScale = 0
                    rigidBody2D.enabledContactListener = true
                    const boxCollider2D = startNode.addComponent(BoxCollider2D)
                    boxCollider2D.size = new Size(DataManager.Instance.position * 2.4, DataManager.Instance.position * 2.2)
                    boxCollider2D.sensor = true
                    let bombLabel = this.node.getChildByName("bombLabel")
                    bombLabel.destroy()
                    let blastSprite = this.node.getComponent(Sprite)
                    let list = DataManager.Instance.mapAssets.jsonAsset.filter(item => item.filename.includes(`blast`))
                    let frame = getFrameAllAsset(DataManager.Instance.mapAssets, list)
                    if (blastSprite) {
                        blastSprite.spriteFrame = frame.find(item => item.name == 'blast21')
                    }
                }
            }, 300)
        }
    }
    onBlastFinished(type: Animation.EventType, state) {
        createPlaceholder(state._targetNode.parent)
        destroyNode(state._targetNode._parent) 
    }
}


